package com.yunxi.controller.sat;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.customer.pojo.CustomerVO;
import com.yunxi.customer.service.CustomerDubboService;
import com.yunxi.dealer.pojo.PartnerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 客户信息 前端控制器
 * </p>
 *
 * @author
 * @since 2019-05-09
 */
@RestController
@RequestMapping("/admin/customer")
@Api(tags = "pms:customer", description = "客户信息接口")
public class CustomerController {
   private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);

   @Autowired
   CustomerDubboService customerDubboService;

   @GetMapping("/page")
   @ApiOperation(value = "查询客户分页列表")
   @ApiImplicitParams({
           @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
           @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
   })
   public ApiResult page(CustomerVO customerVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
       // 关联 pms_partner_customer 表,销售顾问仅能查看自己的客户
       return ResponseUtil.ok(customerDubboService.findPageByVO(customerVO, pageNum, pageSize,loginInfo));
   }

    @GetMapping("/pageByOem")
    @ApiOperation(value = "查询客户分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult pageByOem(CustomerVO customerVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        // 关联 pms_partner_customer 表,销售顾问仅能查看自己的客户
        return ResponseUtil.ok(customerDubboService.pageByOem(customerVO, pageNum, pageSize,loginInfo));
    }


   //PMS上录入手机号通过CRM系统获取用户信息档案
   //CRM没有用户档案时创建用户档案保存到PMS并同步到CRM
   //CRM有用户档案时将用户档案同步到PMS
   //销售在PMS创建用户 PMS系统保存用户档案并且同步给CRM系统
   @PostMapping("/save")
   @ApiOperation(value = "保存客户信息")
   public ApiResult save(@RequestBody CustomerVO customerVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      if(loginInfo == null || loginInfo.getPartner() == null) return ResponseUtil.fail(-1,"请使用渠道账户登陆");
       return customerDubboService.saveOrUpdate(customerVO,loginInfo);
   }

   @PostMapping("/del")
   @ApiOperation(value = "删除")
   public ApiResult del(@RequestBody List<CustomerVO> customerVOList) throws Exception {
      customerDubboService.removeByListVo(customerVOList);
      return ResponseUtil.ok("success");
   }

   @GetMapping("/loadDetail")
   @ApiOperation(value = "通过客户编号获取客户信息详情")
   public ApiResult loadDetail(@RequestParam String customerNo ,String partnerCode, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(customerDubboService.load(customerNo ,partnerCode, loginInfo));
   }

   @GetMapping("/getCustomerByPhone")
   @ApiImplicitParam(paramType="query", name = "phone", value = "手机号", required = true, dataType = "String")
   @ApiOperation(value = "通过手机号获取客户信息")
   public  ApiResult getCustomerByPhone(@RequestParam String phone,@CurrentUser LoginInfo loginInfo) throws Exception{
       return customerDubboService.getCustomerByPhone(phone,loginInfo);
   }

   @GetMapping("/getByPhone")
   @ApiOperation(value = "通过手机号获取客户信息(不调URM)")
   public  ApiResult getCustomerByPhone(@RequestParam String phone,String partnerCode) throws Exception{
      return ResponseUtil.ok(customerDubboService.getByPhone(phone,partnerCode)) ;
   }

    @GetMapping("/allList")
    @ApiOperation(value = "销售顾问本月新增客户")
    public ApiResult allList(CustomerVO customerVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(customerDubboService.allList(customerVO,loginInfo));
    }

    @GetMapping("/todayCustomerList")
    @ApiOperation(value = "今日新增客户列表")
    public ApiResult todayCustomerList(CustomerVO customerVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(customerDubboService.todayCustomerList(customerVO,loginInfo));
    }

   @GetMapping("/list")
   @ApiOperation(value = "查询客户列表")
   public ApiResult list(CustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
      if(loginInfo == null) return ResponseUtil.fail(-1,"请登录");
      return ResponseUtil.ok(customerDubboService.listByVo(v,loginInfo));
   }

}
